package com.sun.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class RestResponse<T> implements Serializable {
    private static final long serialVersionUID = 4893280118017319089L;
    public static final String SUCCESS_CODE = "0";
    public static final String SUCCESS_MESSAGE = System.getProperty("rest.response.success.msg", "操作成功");
    public static final String ERROR_CODE = "9";
    public static final String ERROR_MESSAGE = System.getProperty("rest.response.error.msg", "系统异常");
    private String code;
    private String msg;
    private T data;

    public RestResponse(String code, String msg) {
        this(code, msg, (T) null);
    }

    private RestResponse<T> code(String code) {
        this.setCode(code);
        return this;
    }

    private RestResponse<T> msg(String msg) {
        this.setMsg(msg);
        return this;
    }

    public RestResponse<T> result(T data) {
        this.setData(data);
        return this;
    }

    public boolean success() {
        return "0".equals(this.code);
    }

    public boolean fail() {
        return !this.success();
    }

    public static <T> RestResponse<T> ok(T t) {
        return new RestResponse("0", SUCCESS_MESSAGE, t);
    }

    public static <T> RestResponse<T> ok() {
        return new RestResponse();
    }

    public static <E> RestResponse<E> error(String code, String msg) {
        return new RestResponse(code, msg);
    }

    public static <T> RestResponse<T> error() {
        return new RestResponse("9", ERROR_MESSAGE);
    }

    public static <T> RestResponse<T> error(String msg) {
        return new RestResponse("9", msg);
    }

    public String getCode() {
        return this.code;
    }

    public String getMsg() {
        return this.msg;
    }

    public T getData() {
        return this.data;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof RestResponse)) {
            return false;
        } else {
            RestResponse<?> other = (RestResponse)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                label47: {
                    Object this$code = this.getCode();
                    Object other$code = other.getCode();
                    if (this$code == null) {
                        if (other$code == null) {
                            break label47;
                        }
                    } else if (this$code.equals(other$code)) {
                        break label47;
                    }

                    return false;
                }

                Object this$msg = this.getMsg();
                Object other$msg = other.getMsg();
                if (this$msg == null) {
                    if (other$msg != null) {
                        return false;
                    }
                } else if (!this$msg.equals(other$msg)) {
                    return false;
                }

                Object this$data = this.getData();
                Object other$data = other.getData();
                if (this$data == null) {
                    if (other$data != null) {
                        return false;
                    }
                } else if (!this$data.equals(other$data)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof RestResponse;
    }

    public int hashCode() {
        boolean PRIME = true;
        int result = 1;
        Object $code = this.getCode();
        result = result * 59 + ($code == null ? 43 : $code.hashCode());
        Object $msg = this.getMsg();
        result = result * 59 + ($msg == null ? 43 : $msg.hashCode());
        Object $data = this.getData();
        result = result * 59 + ($data == null ? 43 : $data.hashCode());
        return result;
    }

    public String toString() {
        return "RestResponse(code=" + this.getCode() + ", msg=" + this.getMsg() + ", data=" + this.getData() + ")";
    }
}
